package net.beyondts.mplug;

import net.beyondts.mplug.annotation.Extension;
import net.beyondts.mplug.annotation.Plugin;
import net.beyondts.mplug.utils.MethodUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Project: mplug-project<br/>
 * Description: An exntension handler which handle those methods assigned by @Extension annotation.<br/>
 * Created at: 2019-1-12<br/>
 * Owned by beyondts.
 *
 * @author blazecrystal
 * @version 1.0.0
 */
@Aspect
@Component
public class AnnotatedExtensionHandler extends AbstractExtensionHandler {
    private static Logger LOGGER = LoggerFactory.getLogger(AnnotatedExtensionHandler.class);

    @Around("@annotation(net.beyondts.mplug.annotation.Extension)")
    public Object handle(ProceedingJoinPoint joinPoint) throws Throwable {
        Method extensionMethod = ((MethodSignature) joinPoint.getSignature()).getMethod();
        Extension extension = extensionMethod.getAnnotation(Extension.class);
        try {
            return invokePluginMethod(joinPoint, extension.id());
        } catch (PluginInvocationException e) {
            return invokeExtensionMethod(joinPoint, extension.id());
        }
    }
}
